<?php
/**
 * Created by PhpStorm.
 * User: sevenup
 * Date: 16-4-09
 * Time: 上午10:32
 */
class wechatMenuApic {

    const API_MENU_CREATE = " https://api.weixin.qq.com/cgi-bin/menu/create?access_token=%s";
    const API_MENU_QUERY = "https://api.weixin.qq.com/cgi-bin/menu/get?access_token=%s";
    const API_MENU_DELETE = "https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=%s";

    function createMenu($data) {
        $token = Tools_help::getToken();
        $url = sprintf($this::API_MENU_CREATE, $token);
        $tempStart = "{
                        \"button\":[";
        $tempButton = "{	
                          \"type\":%s,
                          \"name\":%s,
                          \"key\":%s,
                          \"sub_button\":%s
                        },";
        $tempSubButton = "{
                            \"type\": %s, 
                            \"name\": %s, 
                            \"key\": %s, 
                            \"sub_button\": %s,
                            \"media_id\": %s,
                            \"url\":%s
                           },";
        $tempContent = "";
        $tempEnd = "]}";
        foreach ($data as $value) {
            $subButtonContent = null;
            if (!empty($value['sub_button'])) {
                foreach ($value['sub_button'] as $subButton) {
                    $subButtonContent .= sprintf($tempSubButton, $subButton['type']
                        , $subButton['name'], $subButton['key'], $subButton['sub_button']);
                }
            }
            $tempContent .= sprintf($tempButton, $value['type'], $value['name']
                        , $value['key'], $subButtonContent);
        }
        $requestData = $tempStart . $tempContent . $tempEnd;
        return Tools_help::weixinCurlPost($url, $requestData);
    }

    function getMenu() {

    }

    function deleteMenu() {

    }
}